﻿Public Class NewCharacterForm

    Private _token As String
    Private WithEvents _client As New TyranntOnlineCharacterService.TyranntCharacterServiceClient

    Public WriteOnly Property Token() As String
        Set(ByVal value As String)
            _token = value
        End Set
    End Property

    Private Sub newCharacterCancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newCharacterCancelButton.Click
        Me.Close()
    End Sub

    Private Sub newCharacterStartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newCharacterStartButton.Click
        _client.NewCharacterAsync(_token, newCharactersNameTextBox.Text)
    End Sub

    Private Sub newCharactersNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newCharactersNameTextBox.TextChanged
        If newCharactersNameTextBox.Text <> "" Then
            newCharacterStartButton.Enabled = True
        Else
            newCharacterStartButton.Enabled = False
        End If
    End Sub

    Private Sub NewCharacterForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        newCharacterStartButton.Enabled = False
    End Sub

    Private Sub _client_NewCharacterCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.NewCharacterCompletedEventArgs) Handles _client.NewCharacterCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "New Character Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            ElseIf e.Result.Contains("[#]charexist") = True Then
                Me.Text = "Character Name Exits"
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If

        ' New character worked so now launch next form
        Me.Hide()
        Dim selectRace As New selectRaceForm
        selectRace.Init(_token, e.Result)
        selectRace.ShowDialog()
        Me.Close()
    End Sub
End Class